package org.example.computerdesign.service.imp;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.computerdesign.domain.model.PythonRet;
import org.example.computerdesign.domain.po.MedicalCode;
import org.example.computerdesign.domain.po.Patient;
import org.example.computerdesign.domain.vo.MedicalCodeV;
import org.example.computerdesign.exception.ServiceException;
import org.example.computerdesign.mapper.MedicalCodeMapper;
import org.example.computerdesign.mapper.PatientMapper;
import org.example.computerdesign.service.MedicalCodeService;
import org.example.computerdesign.util.PythonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
/**
 * @author 朱文权
 * */
@Service
public class MedicalCodeServiceImp extends ServiceImpl<MedicalCodeMapper, MedicalCode> implements MedicalCodeService {
    @Autowired
    private MedicalCodeMapper mapper;
    @Autowired
    private PatientMapper patientMapper;
    @Override
    public void putCode(String diagnosticCode, String operationCode, String medicineCode, Long pid) {
        Patient patient=patientMapper.selectById(pid);
        if(patient==null){
            throw new ServiceException("病患pid错误",400);
        }
        MedicalCode medicalCode=new MedicalCode();
        medicalCode.setDiagnosticCode(diagnosticCode)
                .setMedicineCode(medicineCode)
                .setOperationCode(operationCode)
                .setPid(pid);
        mapper.insert(medicalCode);
    }

    @Override
    public List<MedicalCode> getCode(Long pid) {
        Patient patient=patientMapper.selectById(pid);
        if(patient==null){
            throw new ServiceException("病患pid错误",400);
        }
        return mapper.selectByPid(pid);
    }

    @Override
    public Integer deleteCode(Long pid) {
        Patient patient=patientMapper.selectById(pid);
        if(patient==null){
            throw new ServiceException("病患pid错误",400);
        }
       return  mapper.deleteByPid(pid);
    }

    @Override
    public List<String> getMedicine(String diagnosticCode, String operationCode, Long pid) {
//        Patient patient=patientMapper.selectById(pid);
//        if(patient==null){
//            throw new ServiceException("病患pid错误",400);
//        }
//        //获取病人病史
//        List<MedicalCode> medicalCodes=mapper.selectByPid(pid);
        //加上本次诊断代码与手术代码
        MedicalCode medicalCode=new MedicalCode();
        medicalCode.setOperationCode(operationCode)
                .setDiagnosticCode(diagnosticCode);
//        medicalCodes.add(medicalCode);
        //向python服务端发请求，获取药物代码数据
        String retJson= PythonUtil.post("/api/process", JSONUtil.toJsonStr(medicalCode));
//        PythonRet pythonRet=JSONUtil.toBean(retJson, PythonRet.class);
        return JSONUtil.toList(retJson,String.class);
    }
}
